<?php

require_once(WSB_MODULES_PATH . 'cms/Url.php');

class Cms_LanguageController extends WSB_Controller_Action
{
	public function init()
	{
		parent::init(); // immer zuerst!
	    $this->_models = array('Cms_Model_DbTable_Language');
	}

	public function addAction()
	{

		$table = new Cms_Model_DbTable_Language();
        $systemLanguages = $table->getSystemLanguages();
        $where = $table->getAdapter()->quoteInto('deleted=?', 0);
        $installedLanguages = $table->fetchAll($where);

	    // Eine Sprache wurde ausgewaehlt und wird gespeichert
	    $iso = strtolower($this->_getParam('iso'));
	    $language = '';
	    if(!empty($iso)) {
	        // Ist die Sprache ist eine gueltige Systemsprache
	        $isValid = false;
	        foreach($systemLanguages AS $sysLanguage) {
                if(strtolower($sysLanguage->iso) == $iso) {
                    $language = (string)$sysLanguage->language;
                    $isValid = true;
                    break;
                }
	        }
	        if(!$isValid) {
				$this->_helper->FlashMessenger->addMessage($this->_helper->Translate('language.error.languageIsNotValid'));
	        }
	        // Ist die Sprache noch nicht installiert
	        if($isValid) {
	            foreach($installedLanguages AS $instLanguage) {
                    if(strtolower($instLanguage->iso) == $iso) {
                        $isValid = false;
						$this->_helper->FlashMessenger->addMessage($this->_helper->Translate('language.error.languageExistsAlready'));
                        break;
                    }
	            }
	        }
	        // Ausgewählte Sprache ist eine gueltige Systemsprache und
	        // wurde noch nicht installiert
            if($isValid) {
    			$table = new Cms_Model_DbTable_Language();
    			$oLanguage = $table->createRow();
    			$oLanguage->iso = $iso;
    			$oLanguage->language = $language;
    			$oLanguage->deleted = 0;
    			$oLanguage->save();
    			WSB_Log::info(sprintf('Added language %s with id %s', $oLanguage->iso, $oLanguage->id));
				$this->_helper->FlashMessenger->addMessage($this->_helper->Translate('language.add.languageAddedSuccessfully'));
            }
			$this->_helper->Redirect($this->getDefaultListUrl());
	    }

	    // Array mit installierbaren Sprachen erstellen
        $installableLanguages = array();
        foreach($systemLanguages AS $systemLanguage) {
            $iso = strtolower($systemLanguage->iso);
            $isInstalled = false;
            foreach($installedLanguages AS $installedLanguage) {
                if(strtolower($installedLanguage->iso) == $iso) {
                    $isInstalled = true;
                    break;
                }
            }
            if(!$isInstalled) {
                array_push($installableLanguages, $systemLanguage);
            }
        }

        $this->view->installableLanguages = $installableLanguages;

        // Settings fuer htmlTable->Viewhelper
		$this->view->columns = array(
			array('key' => 'iso', 'label' => $this->_helper->Translate('iso'), 'sortable' => 'true'),
			array('key' => 'language', 'label' => $this->_helper->Translate('language'), 'sortable' => 'true'),
			array('key' => 'links', 'label' => '')
		);
		$this->view->fields = array(
			array('key' => 'iso'),
			array('key' => 'language'),
			array('key' => 'links', 'callback' => array('Cms_Url', 'languageAddLinkCallback'))
		);
		$this->view->settings = array('sortedBy' => array(
			'key' => 'iso', 'dir' => 'asc')
		);

		if(count($installableLanguages) == 0) {
			$this->render('no-languages');
		}

	}

	/**
	 * Ueberschreibt die Methode in WSB_Controller_Action
	 *
	 * @return string
	 */
    public function getDefaultAddUrl()
	{
		return WSB_Controller_Url::getUrl('language', 'add', null, 'cms');
    }

	/**
	 * Ueberschreibt die Methode in WSB_Controller_Action (kein Editieren)
	 *
	 * @return string
	 */
	/*
	public function buildListItemHtmlLinks(WSB_Db_Row $model, $modelIndex=0)
	{
		$links = array();
		array_push($links, $this->_buildViewListItemHtmlLink($model, $modelIndex));
		array_push($links, $this->_buildDeleteListItemHtmlLink($model, $modelIndex));
		array_push($links, $this->_buildHiddenListItemHtmlLink($model, $modelIndex));
		return $links;
	}
	*/

}
